<?php
// +----------------------------------------------------------------------
// | 产品渠道配置页面
// +----------------------------------------------------------------------
// | Author: TongBC <501468940@qq.com>
// +----------------------------------------------------------------------
// | Date  : 2015/07/30
// +----------------------------------------------------------------------
ob_start();
include_once '/var/www/html/new/d/common/header.html';
include_once '/var/www/html/new/d/common/left.html';
include_once '/var/www/html/new/d/class/Member.php';
$out_html = ob_get_contents();
ob_end_clean();






if(isset($_REQUEST['act']) && $_REQUEST['act']=='lists'){

	$ac = $GLOBALS['_ac'];
	$pw = $GLOBALS['_pw'];
	$s  = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));
	
	$memberSID  = $_SESSION['sid'];
	
	if($memberSID==1) exit(json_encode(array('status'=>'fial', 'msg'=>'账号类型非供应商或分销商')));
	
	$m          = new Member($s, $memberSID);
	$salePros   = $m->salePros(); // 2000 个产品大概 0.6 - 0.8 s
	
	// 景区模糊查询
	$lids = array();
	if(isset($_REQUEST['title']) && $_REQUEST['title']){
		$ltitle = $_REQUEST['title'];
		$sql = "select id from uu_land where title like '%$ltitle%'";
		$GLOBALS['le']->query($sql);
		while($row=$GLOBALS['le']->fetch_assoc()) $lids[] = $row['id'];
	}
	$lids_count = count($lids);// 模糊查询出来的景区ID
	
	// 获取城市地址库
	$areas = array();
	$sql = "select area_id,area_name from uu_area";
	$GLOBALS['le']->query($sql);
	while($row=$GLOBALS['le']->fetch_assoc()) $areas[$row['area_id']] = $row['area_name'];

	// 获取供应商信息
	$supplier = array();
	$sql = "select m.dname,m.qq,m.id,e.com_name from pft_member m left join pft_member_extinfo e on e.fid=m.id where m.dtype=0";
	$GLOBALS['le']->query($sql);
	while($row=$GLOBALS['le']->fetch_assoc()) $supplier[$row['id']] = $row;
	
	$supplier_id = 0;
	// 按供应商搜索
	if(isset($_REQUEST['supplier']) && $_REQUEST['supplier']){
		
		$sql = "select id from pft_member where dname='{$_REQUEST['supplier']}' limit 1";
		$GLOBALS['le']->query($sql);
		if(!$GLOBALS['le']->fetch_assoc()) exit(json_encode(array('status'=>'success', 'lists'=>array(), 'total'=>0)));
		$supplier_id = $GLOBALS['le']->f('id');
		
	}

	// 获取渠道配置
	$channels = array();
	$sql = "select * from pft_sale_channel where fid=$memberSID and status=0";
	$GLOBALS['le']->query($sql);
	while($row=$GLOBALS['le']->fetch_assoc()) $channels[$row['pid'].'_'.$row['aid']] = $row;
	// if($_SESSION['sid']==1433) print_r($channels);
	$channels_t = array(1=>'微信', 2=>'二级店铺', 3=>'自助机');
	
	foreach($salePros as $row){

		if($lids_count && !in_array($row['lid'], $lids)) continue;
		if($supplier_id>0 && $row['sapply_did']!=$supplier_id) continue; 
		$area = explode("|", $row['area']);
		
		// 筛选条件
		if($p_type=="B"){
			$places = explode("|", $row['runtime']);
	
			if($places[0] && !in_array($places[0], $startplaces)) $startplaces[] = $places[0];
			if($places[1] && !in_array($places[1], $endplaces))   $endplaces[]   = $places[1];
		}else{
			if($_REQUEST['provice'] && !in_array($_REQUEST['provice'],$area)) continue;
			if($_REQUEST['city'] && !in_array($_REQUEST['city'],$area)) continue;
			if($area[0]) $province[$area[0]] = $areas[$area[0]];
			if($area[1]) $destination[$area[1]] = $areas[$area[1]];
		}

		
		$row['province'] = '未知';
		$row['city']     = '未知';
		if($row['area']!=''){
			$row['province'] = $areas[$area[0]];
			$row['city']     = $areas[$area[1]];	
		}
		$row['areas'] = $area;
		$products[] = $row;
	}


	$currentPage    = $_REQUEST['currentPage']?abs($_REQUEST['currentPage']):1;
	$pageSize       = $_REQUEST['pageSize']?abs($_REQUEST['pageSize']):10;
	$offset         = ($currentPage-1)*$pageSize;
	$maxRow         = ($currentPage) * $pageSize;

	
	// 拼装数据格式
	$r = array();$date = date('Y-m-d'); $count = 0;
	if(count($products)){
		foreach($products as $row){
		
			if(!isset($r[$row['lid']])){// 景区数据
				$r[$row['lid']] = array(
					'title'    => $row['title'],
					'province' => $row['province'],
					'city'     => $row['city'],
					'areas'    => $row['areas']
				);
				$count++;
			}
			
			// 渠道文本处理
			$channel_t = '';
			$px = 0;
			if(isset($channels[$row['pid'].'_'.$row['sapply_did']])){
				// echo $channels[$row['pid'].'_'.$row['sapply_did']];
				$channel_a = explode(',', $channels[$row['pid'].'_'.$row['sapply_did']]['channel']);
				foreach($channel_a as $chlRow) $channel_t .= $channels_t[$chlRow].',';
				$channel_t = rtrim($channel_t, ',');
				$px = $channels[$row['pid'].'_'.$row['sapply_did']]['px'];
			}
			
			$r[$row['lid']]['list'][] = array(
				'title'   => $row['ttitle'],
				'aid'     => $row['sapply_did'],
				'dname'   => $supplier[$row['sapply_did']]['dname'],
				'lid'     => $row['lid'],
				'pid'     => $row['pid'],
				'channel' => $channel_t,
				'px'      => $px
			);
		}
	}
	
	// if($_SESSION['sid']==47783) {
		// print_r($salePros);
		// print_r($r);
		// exit;
	// }
	
	$total = count($r);
	$response = array();
	if(isset($_REQUEST['set_page'])){
		$page_t  = ceil($total/$pageSize);
		$page_f  = 0;
		$max_row = $offset+$pageSize;
		foreach($r as $row){
			$page_f++;
			if($page_f>$max_row) break;
			if($page_f<$offset) continue;
			$response[] = $row;
		}
		$last = ($currentPage==$page_t) ? '':$currentPage+1;
		exit(json_encode(array('status'=>'success', 'lists'=>$response, 'total'=>$total, 'last'=>$last, 'totalPage'=>$page_t)));
	}else{
		$response = $r;
	}
	
	

	exit(json_encode(array('status'=>'success', 'lists'=>$response, 'total'=>$total)));
	
}// 获取产品结束


 



$memberSID = $_SESSION['sid'];
$isChannel = 0;
$sql = "select id from pft_sale_channel where fid=$memberSID limit 1";
$GLOBALS['le']->query($sql);
if($GLOBALS['le']->fetch_assoc()) $isChannel = 1;

echo $out_html;
include('/var/www/html/new/d/tpl/channels_confd_/channels_conf.tpl');
//include('/var/www/html/new/d/tpl/channel_test/channels_test.html');
include('/var/www/html/new/d/common/footer.html');